package com.zrq.gencode.domain.classdata;

import com.zrq.gencode.cfg.GenCfg;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;

/**
 * @author zhong
 */
public class ClassBase implements Serializable {

	private static final long serialVersionUID = 1L;
	private final LocalDate date = LocalDate.now();
	private Set<String> importList;
	private String className;
	private List<AttributeBase> attributeBases;
	private String author = GenCfg.author;

	public LocalDate getDate() {
		return date;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public List<AttributeBase> getAttributeBases() {
		return attributeBases;
	}

	public void setAttributeBases(List<AttributeBase> attributeBases) {
		this.attributeBases = attributeBases;
	}

	public Set<String> getImportList() {
		return importList;
	}

	public void setImportList(Set<String> importList) {
		this.importList = importList;
	}

	public String getCompleteEmtity() {
		return GenCfg.getCompleteEntity();
	}

	public String getCompleteService() {
		return GenCfg.getCompleteService();
	}

	public String getCompleteServiceImpl() {
		return GenCfg.getCompleteServiceImpl();
	}

	public String getCompleteWeb() {
		return GenCfg.getCompleteWeb();
	}

	public String getCompleteDao() {
		return GenCfg.getCompleteDao();
	}

	public String getEmtityPackageName() {
		return GenCfg.entityPackageName;
	}

	public String getWebPackageName() {
		return GenCfg.webPackageName;
	}

	public String getDaoPackageName() {
		return GenCfg.daoPackageName;
	}

	public String getServicePackageName() {
		return GenCfg.serviceablePackageName;
	}

	public boolean getHasId() {
		return attributeBases.stream()
			.anyMatch(attributeBase -> "id".equals(attributeBase.getName()));
	}

	public String getServiceName() {
		return GenCfg.serviceName.replace("Entity", className);
	}

	public String getServicImplName() {
		return GenCfg.serviceImplName.replace("Entity", className);
	}

	public String getControllerName() {
		return GenCfg.controllerName.replace("Entity", className);
	}

	public String getDaoName() {
		return GenCfg.daoName.replace("Entity", className);
	}

	public String getArg() {
		String arg = className;
		if (className.endsWith("y")) {
			arg = className.substring(0, className.length() - 1) + "ie";
		}
		return arg.substring(0, 1)
			.toLowerCase() + arg.substring(1) + "s";
	}

	public String getIdType() {
		return attributeBases.stream()
			.filter(attributeBase -> "id".equals(attributeBase.getName()))
			.findFirst()
			.map(AttributeBase::getJavaType)
			.orElse("String");
	}
}